package com.bing.controller.admin;

import com.bing.bean.ResultMsg;
import com.bing.bean.TestTable;
import com.bing.entity.Post;
import com.bing.mvc.resolver.JsonObejct;
import com.google.gson.Gson;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Administrator on 2016/11/13.
 */
@Controller
@RequestMapping(Login.LOGIN)
public class Login {
    public static final String LOGIN = "admin";
    public static final Logger LOGGER = LoggerFactory.getLogger(Login.class);

    @RequestMapping(value = "test/{name}.html", method = RequestMethod.GET)
    public String testPath(@PathVariable String name, Model model) {
        LOGGER.info("go to: {}", name);
        return "test/" + name;
    }

    @RequestMapping(value = "{path}", method = RequestMethod.GET)
    public String gotoPath(@PathVariable String path, Model model) {
        LOGGER.info("go to: {}", path);
        return LOGIN + "/" + path;
    }

     @RequestMapping(value = "login", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResultMsg authLogin(@RequestParam String userName, @RequestParam String password) {
        ResultMsg resultMsg = new ResultMsg();
        resultMsg.setStatusCode(200);
        resultMsg.setMessage("登陆成功");
        resultMsg.setForward("http://www.baidu.com");
        return resultMsg;
/*        Subject currentUser = SecurityUtils.getSubject();
        if (!currentUser.isAuthenticated()) {
            UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
            token.setRememberMe(true);
            currentUser.login(token);
        }
        return "success";*/
    }

    @RequestMapping("change")
    @ResponseBody
    public String p1(@RequestParam String userName, @RequestParam String password) {

        LOGGER.info("请求登陆:{},密码:{}", userName, password);
        return "hello";
    }

    @RequestMapping("test")

    public String test(@RequestParam String userName, @RequestParam String password) {

        LOGGER.info("请求登陆:{},密码:{}", userName, password);
        return "hello";
    }

    @RequestMapping(value = "testAdd",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg testAdd(@JsonObejct TestTable json) {

        LOGGER.info("add table:{}", json);

        ResultMsg resultMsg = new ResultMsg();
        resultMsg.setStatusCode(HttpStatus.OK.value());

        return resultMsg;

    }


}
